﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Services_Dispatch : System.Web.UI.Page
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            initDate();
            if (Request["action"] == "add" || Request["action"] == "addedit")
            {
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                ddlName.Visible = false;
                ddlDept.Visible = false;
                lblmaimen.Visible = false;
            }

            if (Request["action"] == "edit" || Request["action"] == "addedit")
            {
                initUser();
                readdata();
            }
            initDept();
            lblDh.Text = initBarCode();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            lblName.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
        }
    }
    private void initUser()
    {
        ZSql z = new ZSql();
        z.Open("select * from CS_Mailadd where Maintaddid=" + Request["id"]).ToString();
        ZSql name = new ZSql();
        name.Open("select StaffId,StaffName,deptId from CS_Staff where StaffId=" + z.m_table.Rows[0]["userid"].ToString());
        ddlComt.SelectedValue = z.m_table.Rows[0]["GuestCom"].ToString();
        ddlDept.SelectedValue = name.m_table.Rows[0]["deptId"].ToString();
        ddlName.DataSource = name.m_table;
        ddlName.DataTextField = "StaffName";
        ddlName.DataValueField = "StaffId";
        ddlName.DataBind();
        name.Close();
        ddlName.SelectedValue = z.m_table.Rows[0]["userid"].ToString();
        z.Close();
    }

    private void initDate()
    {
        txtdate.Text = DateTime.Now.ToShortDateString();
        lblDate.Text = DateTime.Now.ToShortDateString();
        lblDate.Attributes.Add("onclick", "SelectDate('lblDate')");
        txtdate.Attributes.Add("onclick", "SelectDate('txtdate')");
    }
    private void initDept()
    {


        ZSql dept = new ZSql();
        dept.Open("select deptid,deptname from Department");

        ddlDept.DataSource = dept.m_table;
        ddlDept.DataTextField = "deptname";
        ddlDept.DataValueField = "deptid";
        ddlDept.DataBind();

        ddlDept.Items.Insert(0, new ListItem("—选择部门—", "0"));
        ddlName.Items.Insert(0, new ListItem("—选派员工—", "0"));
        dept.Close();
    }
    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        ZSql name = new ZSql();
        name.Open("select StaffId,StaffName from CS_Staff where deptid=" + ddlDept.SelectedValue);
        ddlName.DataSource = name.m_table;
        ddlName.DataTextField = "StaffName";
        ddlName.DataValueField = "StaffId";
        ddlName.DataBind();
        ddlName.Items.Insert(0, new ListItem("—选派员工—", "0"));
        name.Close();
    }

    private string initBarCode()
    {
        ZSql zs = new ZSql();
        string id = "WX" + DateTime.Now.ToString("yyyyMMdd");
        zs.Open("select max(Maintid) as a from CS_Maint");
        if (string.IsNullOrEmpty(zs.m_table.Rows[0]["a"].ToString()))
        {
            id += 1;
        }
        else
        {
            id += Convert.ToInt32(zs.m_table.Rows[0]["a"]) + 1;
        }

        return id;

    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        if (Request["action"] == "add")
        {
            ZSql zs = new ZSql();
            zs.Open("insert into CS_Mailadd(SalesCode,Guestname,Guestmen,Guesttel,Guestaddress,MaintDate,addUserName,dispTime,comidunit,cominame,comidband,comidSpe) values('" + Common.FormatParameter(lblCode.Text) + "','" + Common.FormatParameter(lblGuestName.Text) + "','" + Common.FormatParameter(lblManName.Text) + "','" + Common.FormatParameter(lblPhone.Text) + "','" + Common.FormatParameter(lblAddress.Text) + "','" + Common.FormatParameter(txtdate.Text) + "','" + Common.FormatParameter(lblName.Text) + "','" + Common.FormatParameter(lblDate.Text) + "','" + Common.FormatParameter(lblUnits.Text) + "','" + Common.FormatParameter(lblComoName.Text) + "','" + Common.FormatParameter(lblBrand.Text) + "','" + Common.FormatParameter(lblSpeci.Text) + "')");
            zs.Close();
            Response.Redirect("ServiceGuest.aspx?moduid=" + Request["moduid"]);
        }
        if (Request["action"] == "addedit")
        {
            ZSql zs = new ZSql();
            zs.Open("update CS_Mailadd set SalesCode='" + Common.FormatParameter(lblCode.Text) + "',Guestname='" + Common.FormatParameter(lblGuestName.Text) + "',Guestmen='" + Common.FormatParameter(lblManName.Text) + "',Guesttel='" + Common.FormatParameter(lblPhone.Text) + "',Guestaddress='" + Common.FormatParameter(lblAddress.Text) + "',MaintDate='" + Common.FormatParameter(txtdate.Text) + "',addEditUserName='" + Common.FormatParameter(lblName.Text) + "',dispTime='" + Common.FormatParameter(lblDate.Text) + "',comidunit='" + Common.FormatParameter(lblUnits.Text) + "',cominame='" + Common.FormatParameter(lblComoName.Text) + "',comidband='" + Common.FormatParameter(lblBrand.Text) + "',comidSpe='" + Common.FormatParameter(lblSpeci.Text) + "' where Maintaddid=" + Request["id"] + "");
            zs.Close();
            Response.Redirect("ServiceGuest.aspx?moduid=" + Request["moduid"]);
        }
        if (Request["action"] == "edit")
        {
            ZSql zs = new ZSql();
            zs.Open("update CS_Mailadd set MaintCode='" + lblDh.Text + "',SalesCode='" + Common.FormatParameter(lblCode.Text) + "',Guestname='" + Common.FormatParameter(lblGuestName.Text) + "',Guestmen='" + Common.FormatParameter(lblManName.Text) + "',Guesttel='" + Common.FormatParameter(lblPhone.Text) + "',Guestaddress='" + Common.FormatParameter(lblAddress.Text) + "',MaintDate='" + Common.FormatParameter(txtdate.Text) + "',EditUserName='" + Common.FormatParameter(lblName.Text) + "',Guestconvent='" + Common.FormatParameter(txtGuestYJ.Text) + "',dispTime='" + Common.FormatParameter(lblDate.Text) + "',comidunit='" + Common.FormatParameter(lblUnits.Text) + "',cominame='" + Common.FormatParameter(lblComoName.Text) + "',comidband='" + Common.FormatParameter(lblBrand.Text) + "',comidSpe='" + Common.FormatParameter(lblSpeci.Text) + "',maintdelt='" + Common.FormatParameter(txtbj.Text) + "',maitmen='" + ddlName.SelectedValue + "',GuestCom='" + ddlComt.SelectedValue + "',mailfy=" + Convert.ToDouble(Common.FormatParameter(txtfy.Text)) + ",mailtc=" + Convert.ToDouble(Common.FormatParameter(txttc.Text)) + " where Maintaddid=" + Request["id"] + "");
            zs.Close();
            Response.Redirect("SelectMaintDispatch.aspx?moduid=" + Request["moduid"]);
        }
    }
    protected void readdata()
    {
        ZSql edit = new ZSql();
        edit.Open("select *,Convert(char(10),MaintDate,120) as Date,Convert(char(10),dispTime,120) as Date1 from CS_Mailadd where Maintaddid=" + Request["id"] + "");
        lblDh.Text = edit.m_table.Rows[0]["MaintCode"].ToString();
        lblCode.Text = edit.m_table.Rows[0]["SalesCode"].ToString();
        lblGuestName.Text = edit.m_table.Rows[0]["Guestname"].ToString();
        lblManName.Text = edit.m_table.Rows[0]["Guestmen"].ToString();
        lblPhone.Text = edit.m_table.Rows[0]["Guesttel"].ToString();
        lblAddress.Text = edit.m_table.Rows[0]["Guestaddress"].ToString();
        txtdate.Text = edit.m_table.Rows[0]["Date"].ToString();
        lblName.Text = edit.m_table.Rows[0]["username"].ToString();
        txtGuestYJ.Text = edit.m_table.Rows[0]["Guestconvent"].ToString();
        lblDate.Text = edit.m_table.Rows[0]["Date1"].ToString();
        lblUnits.Text = edit.m_table.Rows[0]["comidunit"].ToString();
        lblComoName.Text = edit.m_table.Rows[0]["cominame"].ToString();
        lblBrand.Text = edit.m_table.Rows[0]["comidband"].ToString();
        lblSpeci.Text = edit.m_table.Rows[0]["comidSpe"].ToString();
        txtbj.Text = edit.m_table.Rows[0]["maintdelt"].ToString();
        lblComoName.Text = edit.m_table.Rows[0]["cominame"].ToString();
        lblState.Text = "已维修";
        txtfy.Text = edit.m_table.Rows[0]["mailfy"].ToString().Substring(0, edit.m_table.Rows[0]["mailfy"].ToString().Length-2);
        txttc.Text = edit.m_table.Rows[0]["mailtc"].ToString().Substring(0, edit.m_table.Rows[0]["mailtc"].ToString().Length - 2);
        edit.Close();
    }
}
